package com.qigao.mall.channel.weichat;

import com.alibaba.fastjson.JSONObject;
import com.qigao.mall.channel.ChannelCore;
import com.qigao.mall.channel.config.ChannelConstant;
import com.qigao.mall.commons.dto.MerChannelDto;
import com.qigao.mall.commons.enums.ChannelActionEnum;
import com.qigao.mall.commons.enums.ChannelCodeEnum;
import com.qigao.mall.commons.enums.ChannelInfoEnum;
import com.qigao.mall.commons.enums.PayProductEnum;
import com.qigao.mall.commons.param.ChannelReqParam;
import com.qigao.mall.commons.param.ChannelRespParam;
import com.qigao.mall.commons.utils.HttpClientUtil;
import org.apache.commons.codec.binary.Base64;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * 下发小程序和公众号统一的服务消息
 * @date:2020-10-27
 * @author:luiz
 * @Description:
 * https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/uniform-message/uniformMessage.send.html
 */
@Service
public class WeichatUniformMsg implements ChannelCore {
    private static Logger logger= LoggerFactory.getLogger(WeichatUniformMsg.class);
    @Resource
    ChannelConstant channelConstant;

    @Override
    public boolean isSupport(PayProductEnum payProductEnum, ChannelActionEnum channelActionEnum, ChannelInfoEnum channelInfoEnum) {
        return ChannelActionEnum.SEND_MSG.equals(channelActionEnum) &&
                PayProductEnum.WEICHAT_SMALL_APP.equals(payProductEnum) &&
                ChannelInfoEnum.WEICHAT.equals(channelInfoEnum)
                ;
    }
    /**
     * "weapp_template_msg":{
     *         "template_id":"TEMPLATE_ID",
     *         "page":"page/page/index",
     *         "form_id":"FORMID",
     *         "data":{
     *             "keyword1":{
     *                 "value":"339208499"
     *             },
     *             "keyword2":{
     *                 "value":"2015年01月05日 12:30"
     *             },
     *             "keyword3":{
     *                 "value":"腾讯微信总部"
     *             },
     *             "keyword4":{
     *                 "value":"广州市海珠区新港中路397号"
     *             }
     *         },
     *         "emphasis_keyword":"keyword1.DATA"
     *     },
     * @param merChannelDto
     * @param channelReqParam
     * @return
     */
    @Override
    public ChannelRespParam channelProcess(MerChannelDto merChannelDto, ChannelReqParam channelReqParam) {
        JSONObject reqContentJson=JSONObject.parseObject(channelReqParam.getReqContent());
        String apiUrl=channelConstant.getWeichatUniformMsgUrl()+"?access_token="+reqContentJson.getString("accessToken");
        JSONObject reqJson=new JSONObject();
        reqJson.put("touser",reqContentJson.getString("recvUserNo"));
        JSONObject weappJson=new JSONObject();
        weappJson.put("template_id",reqContentJson.getString("templateId"));
        weappJson.put("page",reqContentJson.getString("page"));
        weappJson.put("form_id",reqContentJson.getString("formId"));
        weappJson.put("emphasis_keyword",reqContentJson.getString("emphasisKeyword"));
        JSONObject dataJson=reqContentJson.getJSONObject("dataJson");
        weappJson.put("data",dataJson);
        reqJson.put("weapp_template_msg",weappJson);
        logger.info("[下发消息]:{},{}",apiUrl,reqJson);
        String respContent=HttpClientUtil.doPost(apiUrl,reqJson);
        logger.info("微信接口凭证获取返回内容:{}",respContent);
        ChannelRespParam channelRespParam=new ChannelRespParam();
        channelRespParam.setRespCode(ChannelCodeEnum.SUCCESS.getCode());
        channelRespParam.setRespMsg(ChannelCodeEnum.SUCCESS.getName());
        return channelRespParam;
    }

}
